﻿using Ewell.BizComponent.UI.Core.VievModel;
using Ewell.BizComponent.UI.Reserve.ViewModel;
using Ewell.Interface.Common.Model.GateWay.Dto.Basic;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Ewell.Interface.Common.Utils;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay.Dto.Dict;
using Ewell.BizComponent.UI.Core.Model;

namespace Ewell.BizComponent.UI.Reserve.Windows
{
    /// <summary>
    /// OutpReserveWindow.xaml 的交互逻辑
    /// </summary>
    public partial class EncounterReserveWindow : MetroWindow
    {
        private EncounterReserveVm _encounterReserveVm;
        public EncounterReserveVm encounterReserveVm
        {
            get => _encounterReserveVm;
            set
            {
                if(_encounterReserveVm != value)
                {
                    _encounterReserveVm = value;
                    OnPropertyChanged(nameof(encounterReserveVm));
                }
            }
        }
        public EncounterReserveWindow()
        {
            InitializeComponent();
            encounterReserveVm = new EncounterReserveVm();
            
            this.DataContext= this;
        }

        public EncounterReserveWindow(OutpReserveRequestVm outpReserveRequestVm) : this()
        {
            encounterReserveVm = new EncounterReserveVm() { outpReserveRequestVm= outpReserveRequestVm };
            #region 查询患者信息
            if (!string.IsNullOrWhiteSpace(outpReserveRequestVm.patientId))
            {
                PersonDubboService personDubboService = new PersonDubboService();
                PersonPatientQueryRequest patientQueryRequest = new PersonPatientQueryRequest()
                {
                    patientIds = new List<string>() { outpReserveRequestVm.patientId },
                    personInfoTypes = new List<string>() { "2" },
                };
                var gateWayPatList = personDubboService.queryPersonByExample.Post<List<PatientVm>>(patientQueryRequest).CheckState("诊间挂号");
                if (gateWayPatList.@object == null || gateWayPatList.@object.Count == 0) throw new SpeedException("HISBIZ", "未查询到患者信息!");
                encounterReserveVm.patientBannerVm.patientVm = gateWayPatList.@object.FirstOrDefault();
                encounterReserveVm.outpReserveVm.patientVm = gateWayPatList.@object.FirstOrDefault();
            }
            #endregion

            #region 科室编码转换
            if (!string.IsNullOrWhiteSpace(outpReserveRequestVm.deptCode))
            {
                DictDivisionQueryRequest queryDivReq = new DictDivisionQueryRequest()
                {
                    codes = new List<string>() { }
                };
                DictDivisionDubboService dictDivisionDubboService = new DictDivisionDubboService();
                var gateWayDiv = dictDivisionDubboService.queryPageDictDivision.Post<List<DictDivisionResponse>>(queryDivReq);
                if (!gateWayDiv.success) throw new SpeedException("诊间挂号", gateWayDiv.message);
                if (gateWayDiv.@object?.Count <= 0) throw new SpeedException("诊间挂号", $"未查询到{outpReserveRequestVm.deptCode}的科室!");
                encounterReserveVm.outpReserveRequestVm.deptId = gateWayDiv.@object.FirstOrDefault().no;
            }
            #endregion

            #region 人员编码转换
            if (!string.IsNullOrWhiteSpace(outpReserveRequestVm.docCode))
            {
                DictUserQueryRequest userQueryRequest = new DictUserQueryRequest()
                {
                    codes = new List<string>() { outpReserveRequestVm.docCode }
                };
                DictCloudUserDubboService dictCloudUserDubboService = new DictCloudUserDubboService();
                var gateWayUser = dictCloudUserDubboService.queryPageDictUser.Post<List<DictUserResponse>>(userQueryRequest);
                if (!gateWayUser.success) throw new SpeedException("诊间挂号", gateWayUser.message);
                if (gateWayUser.@object?.Count <= 0) throw new SpeedException("诊间挂号", $"未查询到{outpReserveRequestVm.docCode}的医生!");
                encounterReserveVm.outpReserveRequestVm.docId = gateWayUser.@object.FirstOrDefault().no;
            }
            #endregion
            encounterReserveVm.outpReserveVm.outpReserveRequestVm = encounterReserveVm.outpReserveRequestVm;
            encounterReserveVm.outpReserveVm.localWindow = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(Global.RuntimeEnv!="System")
                throw new SpeedException("HISBIZ","用户取消了操作!");
            this.Close();
        }
    }
}
